﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using EnvDTE80;

namespace BovespaDesignRules
{
    public sealed class Rule_ServiceNamesMustMatchInterfaceNames : BaseAddIn.Rules.BaseRuleAddIn
    {
        #region Constructor

        public Rule_ServiceNamesMustMatchInterfaceNames()
            : base("BovespaServiceNamesMustMatchInterfaceNames", "BovespaDesignRules.BovespaDesignRules", typeof(Rule_ServiceNamesMustMatchInterfaceNames).Assembly)
        {
        }

        #endregion

        #region Protected Override Methods

        protected sealed override void Check(CodeClass2 classe)
        {
            CodeInterface interfaceServico;
            CodeAttribute atributoInter;
            bool breakLoop = false;

            if (classe.ImplementedInterfaces.Count > 0)
            {
                for (int i = 1; i <= classe.ImplementedInterfaces.Count; i++)
                {
                    interfaceServico = (CodeInterface)classe.ImplementedInterfaces.Item(i);

                    for (int j = 1; j <= interfaceServico.Attributes.Count; j++)
                    {
                        atributoInter = (CodeAttribute)interfaceServico.Attributes.Item(j);

                        if (atributoInter.Name == "ServiceContract")
                        {
                            if (interfaceServico.Name.Substring(1) != classe.Name)
                            {
                                GeraProblemaAddIn(classe.StartPoint.Parent.Parent.FullName,
                                                 classe.StartPoint.Line,
                                                 classe.StartPoint.DisplayColumn,
                                                 "ServiceNamesMustMatchInterfaceNamesResolution",
                                                 classe.Name,
                                                 interfaceServico.Name,
                                                 classe.Namespace.Name);
                            }

                            breakLoop = true;
                            break;
                        }
                    }

                    if (breakLoop)
                        break;
                }
            }
        }

        #endregion
    }
}
